// pages/index/activity/index.js
import Dialog from '@vant/weapp/dialog/dialog';
const util = require('../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nbTitle: '报名活动',
    nbLoading: false,
    nbFrontColor: '#ffffff',
    nbBackgroundColor: '#3975C6',
    activityDetail: {
      activityTitle:'鹿鸣湖壹号幼儿园火爆招生中',
      activityContent:'专业的教育，温馨的环境，为您的孩子提供一个最佳的学习和生活场所。欢迎加入我们的幼儿园，一起为孩子的明天添砖加瓦！',
      activityImg:'https://ss2.meipian.me/users/57375071/5a81ed01a956a82d0bc4013ed894d912.jpg?meipian-watermark/bucket/ivwen/key/dXNlcnMvNTczNzUwNzEvNWE4MWVkMDFhOTU2YTgyZDBiYzQwMTNlZDg5NGQ5MTIuanBn/nickname/6YGH6KeB/userid/NTczNzUwNzE=/sign/1b9771f949e8fffee20af9cea14454b4%7cimageView2/2/w/750/interlace/0/q/80',
      activityShareImg:'https://ss2.meipian.me/users/57375071/5a81ed01a956a82d0bc4013ed894d912.jpg?meipian-watermark/bucket/ivwen/key/dXNlcnMvNTczNzUwNzEvNWE4MWVkMDFhOTU2YTgyZDBiYzQwMTNlZDg5NGQ5MTIuanBn/nickname/6YGH6KeB/userid/NTczNzUwNzE=/sign/1b9771f949e8fffee20af9cea14454b4%7cimageView2/2/w/750/interlace/0/q/80',
      activityWxShareUrl:'../images/ozf8D5QS4HAXx_FKNfpsSifYntzo.jpg',
    } ,
    startTime:'2024年1月1日',
    endTime:'2024年5月1日',
    showModal: false,
    showLoading: false,
    isSubmiting: false
  },
  isShowModal: function () {
    this.setData({
      showModal: false
    })
    console.log("showModal", this.data.showModal)
  },
  apply() {
      if (this.data.isSubmiting) {
        return
      }
      wx.navigateTo({
        url: "/pages/sq-in/index",
      })
  },
  formatDate: function (date) {
    return (date + "").replaceAll("-", ".")
  },
  share() {
    const sysInfo = wx.getSystemInfoSync()
    const environment = sysInfo.environment
    if (sysInfo.platform.includes('windows') || sysInfo.platform.includes('mac')) {
      Dialog.alert({
        message: '分享功能在PC端微信下不可用!',
      }).then(() => {});
      return
    }
    if (environment) {
      Dialog.alert({
        message: '分享功能在企业微信下不可用!',
      }).then(() => {});
      return
    }
    let that=this;
    that.setData({
      showModal: true,
      showLoading: true
    })
    setTimeout(() => {
      wx.createSelectorQuery()
        .select('#myCanvas')
        .fields({
          node: true,
          size: true,
        })
        .exec((res) => {
          const canvas = res[0].node
          // 渲染上下文
          const ctx = canvas.getContext('2d')
          // Canvas 画布的实际绘制宽高
          const width = res[0].width
          const height = res[0].height
          console.log(width, height)
          // 初始化画布大小
          const sysInfo = wx.getSystemInfoSync()
          const dpr = sysInfo.pixelRatio
          canvas.width = width * dpr
          canvas.height = height * dpr
          ctx.scale(dpr, dpr)

          ctx.fillStyle = '#fff'
          ctx.fillRect(0, 0, canvas.width, canvas.height)

          let image = canvas.createImage()
          image.onload = (res) => {
            console.log("res1", res)
            ctx.drawImage(
              image,
              0,
              0,
              canvas.width / dpr,
              canvas.height / dpr - 210 / dpr,
            )
            let image2 = canvas.createImage()
            image2.onload = (res) => {
              console.log("res", res)
              ctx.drawImage(
                image2,
                20 / dpr,
                canvas.height / dpr - 200 / dpr,
                180 / dpr,
                180 / dpr
              )
              ctx.fillStyle = '#000'
              ctx.font = "normal 0.9em Arial"
              if (that.data.activityDetail.activityTitle.toString().length > 15) {
                ctx.fillText(that.data.activityDetail.activityTitle.toString().substring(0, 7 * dpr), 230 / dpr,
                  canvas.height / dpr - 160 / dpr, 100 * dpr)
                ctx.fillText(that.data.activityDetail.activityTitle.toString().substring(7 * dpr, 7 * dpr * 2), 230 / dpr,
                  canvas.height / dpr - 130 / dpr, 100 * dpr)
              } else {
                ctx.fillText(that.data.activityDetail.activityTitle, 230 / dpr,
                  canvas.height / dpr - 160 / dpr)
              }
              ctx.font = "normal 0.9em Arial"
              ctx.fillText('长按二维码识别进入活动页面', 230 / dpr,
                canvas.height / dpr - 80 / dpr, )
              that.setData({
                showLoading: false
              })
            }
            image2.src = that.data.activityDetail.activityWxShareUrl
          }
          image.src = that.data.activityDetail.activityShareImg
        })
    }, 500)
  },
  saveAndShare() {
    if (this.data.showLoading) {
      return
    }
    const sysInfo = wx.getSystemInfoSync()
    const environment = sysInfo.environment
    if (sysInfo.platform.includes('windows') || sysInfo.platform.includes('mac')) {
      Dialog.alert({
        message: '分享功能在PC端微信下不可用!',
      }).then(() => {});
      return
    }
    if (environment) {
      Dialog.alert({
        message: '分享功能在企业微信下不可用!',
      }).then(() => {});
      return
    }
    let that = this
    wx.createSelectorQuery()
      .select('#myCanvas')
      .fields({
        node: true,
        size: true,
      })
      .exec((res) => {
        const canvas = res[0].node
        const width = res[0].width
        const height = res[0].height
        console.log(width, height)
        wx.canvasToTempFilePath({
          canvas: canvas,
          success(res) {
            let urls = res.tempFilePath //图片临时路径
            wx.showShareImageMenu({ //分享给朋友
              path: urls,
              success: (res) => {
                console.log("分享成功：", res);
                that.setData({
                  showModal: false
                })
              },
              fail: (err) => {
                console.log("分享失败：", err);
                wx.showToast({
                  title: "分享失败",
                  duration: 2000
                })
                that.setData({
                  showModal: false
                })
              },
            })
          }
        })
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let activityId = options.activityId
    console.log("直接访问的路径的activityId", activityId)
    const q = decodeURIComponent(options.q)
    console.log("路径", q)
    let urlParam = undefined
    console.log("判断", q, q.toString() != 'undefined', q != undefined)
    if (q && q.toString() != 'undefined' && q != undefined) {
      urlParam = q.toString().split("?")[1].split("=")[1]
    }
    console.log("参数", urlParam)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log("onShow")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.activityDetail.activityTitle
    }
  }
})